package cn.wolfcode.controller;


import cn.wolfcode.domain.MessageBoardZlb;
import cn.wolfcode.domain.MessageReplyZlb;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.mapper.MessageReplyZlbMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.IMessageBoardZlbService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/messageBoardZlb")
public class MessageBoardZlbController {

    @Autowired
    private IMessageBoardZlbService messageBoardZlbService;
@Autowired
    private MessageReplyZlbMapper messageReplyZlbMapper;
@Autowired
private ISystemDictionaryItemService systemDictionaryItemService;

    @RequiredPermission(name="留言页面",expression="messageBoardZlb:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<MessageBoardZlb> pageInfo = messageBoardZlbService.query(qo);
        List<SystemDictionaryItem> business = systemDictionaryItemService.queryBySn("business");
        model.addAttribute("business",business);
        model.addAttribute("PageInfo", pageInfo);
        return "messageBoardZlb/list";
    }


    @RequiredPermission(name="留言删除",expression="messageBoardZlb:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           messageBoardZlbService.delete(id);
        }
        return "redirect:/messageBoardZlb/list";
    }


    @RequiredPermission(name="留言新增/编辑",expression="messageBoardZlb:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(MessageBoardZlb messageBoardZlb) {
        if (messageBoardZlb.getId() == null) {
            messageBoardZlbService.save(messageBoardZlb);
        } else {
            messageBoardZlbService.update(messageBoardZlb);
        }
        return "redirect:/messageBoardZlb/list";
    }


    @RequiredPermission(name="留言新增/编辑",expression="messageBoardZlb:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("messageBoardZlb",messageBoardZlbService.get(id));
        }
        return "messageBoardZlb/input";
    }
    @RequestMapping("/check")
    public String check(Long id,Model model){
        MessageBoardZlb message = messageBoardZlbService.get(id);
        List<MessageReplyZlb> items=messageReplyZlbMapper.queryByMessageId(id);
        model.addAttribute("message",message);
        model.addAttribute("items",items);
        model.addAttribute("countReplyMessage",items.size());
        return "messageBoardZlb/check";
    }

    @RequestMapping("/selectSmallSys")
    @ResponseBody
    public List selectSmallSys(Long parentId){
        SystemDictionaryItemQueryObject qo = new SystemDictionaryItemQueryObject();
        qo.setParentId(parentId);
        qo.setPageSize(0);
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        return pageInfo.getList();
    }

    @RequestMapping("/save")
    public String save(MessageBoardZlb messageBoardZlb){
        messageBoardZlb.setCreateTimeZlb(new Date());
        messageBoardZlb.setReplystatusZlb(false);
        messageBoardZlbService.save(messageBoardZlb);
        return "redirect:/messageBoardZlb/list";
    }
}
